package com.example.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.*;
import com.example.auth.model.RoleUserModel;
import com.example.auth.model.UserModel;
import com.example.auth.model.vo.RoleUserModelVo;
import com.example.auth.service.RoleUserModelService;
import com.example.auth.service.UserModelService;
import com.example.db.model.IPage;
import com.example.utils.Results;

import java.util.Date;
import java.util.List;

/**
 * @Classname RoleUserModelController
 * @Description TODO
 * @Date 2022/7/31 15:51
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController("/roleusermodel")
public class RoleUserModelController {

    @Autowired
    RoleUserModelService roleUserModelService;

    @Autowired
    UserModelService userModelService;

    @PostMapping(value = "/page",desc = "分页")
    public JSONObject pageController(@Param(value = "pageNum",desc = "页码",defalultValue = "1")Integer pageNum,
                                     @Param(value = "pageSize",desc = "条数",defalultValue = "10")Integer pageSize,
                                     @Param(value = "searchParams",required = false)JSONObject object
    ){
        IPage<RoleUserModel> iPage=roleUserModelService.page(pageNum,pageSize,object);
        return Results.successLayuiTable(iPage);
    }

    @GetMapping(value = "/getone",desc = "获取单个")
    public JSONObject getRoleUserModelOne(@Param(value = "id")String id){
        RoleUserModel userModel=roleUserModelService.getRoleUserModelById(id);
        return Results.success(userModel);
    }

    @GetMapping(value = "/getRoleUserModelRoleIdListByUid",desc = "查询用户已经授权的角色id")
    public JSONObject getRoleUserModelRoleIdListByUid(@Param("uid")String uid){
        List<Integer> ids=roleUserModelService.getRoleModelRoleIdsByUid(uid);
        return Results.success(ids);
    }

    @PostMapping(value = "/saveRoleUserModel",desc = "新增用户和角色的关联")
    public JSONObject saveRoleUserModelController(@RequestBody RoleUserModelVo roleUserModelVo){
        if (roleUserModelVo == null) {
            return Results.error("信息不得为空");
        }
        UserModel userModel=userModelService.getUserModelById(String.valueOf(roleUserModelVo.getUid()));
        if(userModel == null){
            return Results.error("用户信息不存在");
        }
        List<Integer> roleIds=roleUserModelVo.getRoleIds();
        if(roleIds==null){
            return Results.error("信息不得为空");
        }
        List<Integer> roleIdsOld=roleUserModelService.getRoleModelRoleIdsByUid(userModel.getId());
        for (int i = 0; i < roleIdsOld.size(); i++) {
            Integer roleId=roleIdsOld.get(i);
            if(!roleIds.contains(roleId)){
                //不存在则清除
                roleUserModelService.deleteRoleUserModelByUidAndRoleId(userModel.getId(),roleId);
            }else {
                //包含则不添加
                roleIds.remove(roleId);
            }
        }
        RoleUserModel roleUserModel=null;
        //添加
        for (int i = 0; i < roleIds.size() ; i++) {
            Integer roleId = roleIds.get(i);
            RoleUserModel roleUser=roleUserModelService.getRoleUserModelByUidAndRoleId(userModel.getId(),
                    roleId);
            if(roleUser!=null){
                continue;
            }
            roleUserModel=new RoleUserModel();
            roleUserModel.setUserid(userModel.getId());
            roleUserModel.setRoleId(roleId);
            roleUserModel.setCreatedate(new Date());
            roleUserModelService.saveRoleUserModel(roleUserModel);
        }
        return Results.success("保存成功");
    }
}
